#ifndef __FAKETCP_H__
#define __FAKETCP_H__

#include <QObject>
#include <QUdpSocket>
#include <QString>
#include <QMutex>
#include <QWaitCondition>
#include <QMap>
#include <QList>
#include <QPair>

class FakeTcp;

#include "sendthread.h"
#include "packet.h"
#include "packetdata.h"

class FakeTcp: public QObject {

Q_OBJECT

public:
	FakeTcp(QString destination, quint16 port, bool bindPort = true, quint16 localPort = 10000);
	~FakeTcp();

	void sendMessage(QString message);
	bool hasReadyMessage();
	QString getMessage();
	QString getMessage(QString& ip, quint16& port);
	void sendPart(QByteArray data);
	QString getDestination();
	quint16 getPort();
signals:
	void messageReady();
private slots:
	void readData();
private:
	QByteArray packetData(quint16 answerPort, quint32 partNumber, quint32 totalParts, quint32 packetCode, char flags, QString message);

	QUdpSocket socket;
	SendThread * sendThread;

	QString destination;
	quint16 port;
	quint16 localPort;
	
	quint32 messageCode;
	QMutex packetMutex;
	QWaitCondition packetCondition;

	QList<QPair<QString, QPair<QString, quint16> > > readyMessages;
	QList<Packet*> sendingPackets;
	QMap<QPair<quint32, QPair<QString, quint16> >, PacketData*> incompletePackets;

	static QMutex socketsMutex;
	static QMap<QPair<QString, quint16>, FakeTcp*> availableSockets;
};

#endif
